home *** CD-ROM | disk | FTP | other *** search
- /*
- * Box for the utility library
- *
- * This is a simplified version of the box application that is described in
- * develop issue 22.
- *
- * It uses the QuickDraw 3D utility library
- *
- * Nick Thompson, nickt@apple.com
- * Send bugs to devsupport@apple.com
- *
- */
-
- #include <stdlib.h>
-
- #include "Q3UL.h"
-
- #include "QD3D.h"
- #include "QD3DGeometry.h"
- #include "QD3DGroup.h"
- #include "QD3DMath.h"
- #include "QD3DShader.h"
- #include "QD3DTransform.h"
-
-
- #define MYIDTYPE theWindowRef
-
- /*------------------------------------------------------------------------------
- * types.
- */
-
- typedef struct DocumentRec {
- TQ3GroupObject fModel ; /* some geometry to draw */
- TQ3Matrix4x4 fRotation; /* the transform for the model */
- } DocumentRec ;
-
- void MyInitWindowData( TQ3UL_WindowRef theWindowRef ) ;
- TQ3GroupObject MyNewModel( void ) ;
-
- void MyCloseHandler( TQ3UL_WindowRef theWindowRef ) ;
- void MyRedrawHandler( TQ3UL_WindowRef windowID, TQ3ViewObject viewRef ) ;
- void MyIdleHandler( TQ3UL_WindowRef windowID ) ;
-
- /*------------------------------------------------------------------------------
- *
- */
-
- int main(void)
- {
- if( Q3UL_Initialize() == kQ3Success )
- {
- /* make a new window */
- TQ3UL_WindowRef theWindowRef ;
-
- theWindowRef = Q3UL_NewWindow( 400, 400 ) ;
-
- /* initlialize the window data */
- MyInitWindowData( theWindowRef ) ;
-
- /* call the library main event loop */
- Q3UL_MainEventLoop() ;
- }
- }
-
- /*------------------------------------------------------------------------------
- *
- */
-
- void MyInitWindowData(TQ3UL_WindowRef theWindowRef)
- {
- DocumentRec *myDocumentRec = (DocumentRec *)malloc(sizeof(DocumentRec)) ;
-
- /* the identifier is not really used in this app, in a larger app, it would probably
- * be used to switch between window/document types
- */
- long myIdentifier = MYIDTYPE ;
-
- myDocumentRec->fModel = MyNewModel() ; /* the main display group */
- Q3Matrix4x4_SetIdentity(&myDocumentRec->fRotation); /* set to the identity matrix */
-
- /* set the identifier for this app and stash the document record away */
- Q3UL_SetPrivType( theWindowRef, myIdentifier ) ;
- Q3UL_SetPrivData( theWindowRef, (void *)myDocumentRec ) ;
-
- /* install handlers for the window */
- Q3UL_RegisterRedraw( theWindowRef, MyRedrawHandler ) ;
- Q3UL_RegisterIdle( theWindowRef, MyIdleHandler ) ;
- Q3UL_RegisterCloseWindowHandler( theWindowRef, MyCloseHandler ) ;
-
- }
-
-
- /*------------------------------------------------------------------------------
- *
- */
-
- void MyCloseHandler(TQ3UL_WindowRef theWindowRef)
- {
- unsigned long thisType ;
-
- /*
- * get the private data from the window reference
- * first ensuring that the document is the right type.
- */
-
- thisType = Q3UL_GetPrivType( theWindowRef ) ;
-
- if( thisType == MYIDTYPE )
- {
- DocumentRec *thisDocument ;
- thisDocument = (DocumentRec *)Q3UL_GetPrivData( theWindowRef ) ;
-
- if( thisDocument->fModel != NULL )
- Q3Object_Dispose( thisDocument->fModel ) ;
-
- if( thisDocument != NULL )
- free( thisDocument ) ;
-
- /* the user clicked in the close box,
- * quit and bail
- */
- }
-
- Q3UL_DestroyWindow( theWindowRef ) ;
- Q3UL_Terminate() ;
- }
-
- /*------------------------------------------------------------------------------
- * .
- */
-
- void MyRedrawHandler( TQ3UL_WindowRef windowID, TQ3ViewObject viewRef )
- {
- DocumentRec *thisDocument ;
-
- thisDocument = (DocumentRec *)Q3UL_GetPrivData( windowID ) ;
- if( thisDocument != NULL )
- {
- Q3View_StartRendering( viewRef );
- do {
- Q3MatrixTransform_Submit( &thisDocument->fRotation, viewRef );
- Q3DisplayGroup_Submit( thisDocument->fModel, viewRef );
- } while (Q3View_EndRendering( viewRef ) == kQ3ViewStatusRetraverse );
- }
- }
-
- /*------------------------------------------------------------------------------
- *
- */
-
- void MyIdleHandler(
- TQ3UL_WindowRef windowID )
- {
- TQ3Matrix4x4 tmp;
- DocumentRec *thisDocument ;
-
- thisDocument = (DocumentRec *)Q3UL_GetPrivData( windowID ) ;
- if( thisDocument != NULL )
- {
- Q3Matrix4x4_SetRotate_XYZ(&tmp, 0.1, 0.12, 0.08);
- Q3Matrix4x4_Multiply(&thisDocument->fRotation, &tmp, &thisDocument->fRotation) ;
- }
- Q3UL_RedrawWindow( windowID ) ;
-
- }
-
-
- /*------------------------------------------------------------------------------
- *
- */
-
-
- void MyColorBoxFaces(
- TQ3BoxData *myBoxData )
- {
- TQ3ColorRGB faceColor ;
- short face ;
-
- /* sanity check - you need to have set up
- * the face attribute set for the box data
- * before calling this.
- */
-
- if( myBoxData->faceAttributeSet == NULL )
- return ;
-
- /* make each face of a box a different color */
-
- for( face = 0; face < 6; face++) {
-
- myBoxData->faceAttributeSet[face] = Q3AttributeSet_New();
- switch( face ) {
- case 0:
- faceColor.r = 1.0;
- faceColor.g = 0.0;
- faceColor.b = 0.0;
- break;
-
- case 1:
- faceColor.r = 0.0;
- faceColor.g = 1.0;
- faceColor.b = 0.0;
- break;
-
- case 2:
- faceColor.r = 0.0;
- faceColor.g = 0.0;
- faceColor.b = 1.0;
- break;
-
- case 3:
- faceColor.r = 1.0;
- faceColor.g = 1.0;
- faceColor.b = 0.0;
- break;
-
- case 4:
- faceColor.r = 1.0;
- faceColor.g = 0.0;
- faceColor.b = 1.0;
- break;
-
- case 5:
- faceColor.r = 0.0;
- faceColor.g = 1.0;
- faceColor.b = 1.0;
- break;
- }
- Q3AttributeSet_Add(myBoxData->faceAttributeSet[face],
- kQ3AttributeTypeDiffuseColor, &faceColor);
- }
- }
-
- /*------------------------------------------------------------------------------
- *
- */
-
- TQ3GroupPosition MyAddTransformedObjectToGroup(
- TQ3GroupObject theGroup,
- TQ3Object theObject,
- TQ3Vector3D *translation )
- {
- TQ3TransformObject transform;
-
- transform = Q3TranslateTransform_New(translation);
- Q3Group_AddObject(theGroup, transform);
- Q3Object_Dispose(transform);
- return Q3Group_AddObject(theGroup, theObject);
- }
-
- /*------------------------------------------------------------------------------
- *
- */
-
- TQ3GroupObject MyNewModel( void )
- {
- TQ3GroupObject myGroup = NULL;
- TQ3GeometryObject myBox;
- TQ3BoxData myBoxData;
- TQ3ShaderObject myIlluminationShader ;
- TQ3Vector3D translation;
-
- TQ3SetObject faces[6] ;
- short face ;
-
- /* Create a group for the complete model.
- * do not use Q3OrderedDisplayGroup_New since in this
- * type of group all of the translations are applied before
- * the objects in the group are drawn, in this instance we
- * dont want this.
- */
- if ((myGroup = Q3DisplayGroup_New()) != NULL ) {
-
- /* Define a shading type for the group
- * and add the shader to the group
- */
- myIlluminationShader = Q3PhongIllumination_New();
- Q3Group_AddObject(myGroup, myIlluminationShader);
-
- /* set up the colored faces for the box data */
- myBoxData.faceAttributeSet = faces;
- myBoxData.boxAttributeSet = nil;
- MyColorBoxFaces( &myBoxData ) ;
-
- /* create the box itself */
- Q3Point3D_Set(&myBoxData.origin, 0, 0, 0);
- Q3Vector3D_Set(&myBoxData.orientation, 0, 1, 0);
- Q3Vector3D_Set(&myBoxData.majorAxis, 0, 0, 1);
- Q3Vector3D_Set(&myBoxData.minorAxis, 1, 0, 0);
- myBox = Q3Box_New(&myBoxData);
-
- /* put four copies of the box into the group, each one with its own translation */
- translation.x = 0;translation.y = 0;translation.z = 0;
- MyAddTransformedObjectToGroup( myGroup, myBox, &translation ) ;
-
- translation.x = 2;translation.y = 0;translation.z = 0;
- MyAddTransformedObjectToGroup( myGroup, myBox, &translation ) ;
-
- translation.x = 0;translation.y = 0;translation.z = -2;
- MyAddTransformedObjectToGroup( myGroup, myBox, &translation ) ;
-
- translation.x = -2;translation.y = 0;translation.z = 0;
- MyAddTransformedObjectToGroup( myGroup, myBox, &translation ) ;
- }
-
- /* dispose of the objects we created here */
- if( myIlluminationShader )
- Q3Object_Dispose(myIlluminationShader);
-
- for( face = 0; face < 6; face++) {
- if( myBoxData.faceAttributeSet[face] != NULL )
- Q3Object_Dispose(myBoxData.faceAttributeSet[face]);
- }
-
- if( myBox )
- Q3Object_Dispose( myBox );
-
- return ( myGroup );
- }
-
-
-